package com.buybuyyi.stylistcrm.resource.controllers.user;

import com.buybuyyi.stylistcrm.resource.controllers.DefaultController;
import com.buybuyyi.stylistcrm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import stylistcrm.core.query.ResultPagerModel;
import stylistcrm.core.services.order.enums.EnumExceptionCode;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.security.dto.UserDto;
import stylistcrm.core.services.user.User;
import stylistcrm.core.services.user.requisition.QueryRequisitionConditionDto;
import stylistcrm.core.services.user.requisition.RequisitionDto;
import stylistcrm.core.services.user.service.RequisitionService;
import stylistcrm.core.services.user.service.UserService;

import java.util.List;

/**
 * Created by cheny on 2016/10/15.
 */
@Controller
@RequestMapping("/user")
public class UserManageController extends DefaultController{

    /**
     * 申请单管理service
     */
    @Autowired
    RequisitionService requisitionService;

    /**
     * 注册通用账号
     * @param userName
     * @param loginName
     * @param password
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/registeraccount")
    public UserDto registerAccount(String userName,String loginName,String password) throws BizException{
        if(StringUtil.isBlank(userName)||StringUtil.isBlank(loginName)||StringUtil.isBlank(password)){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        return userService.registerAccount(userName,loginName,password);
    }

    /**
     * 注册设计师
     * @param designer
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/registerdesigner")
    public Boolean registerDesigner(RequisitionDto designer) throws BizException{
        Long currentUserId=checkLogin();
        if(null == designer){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        designer.setSecUserId(currentUserId);
        return requisitionService.registerRequisition(designer);
    }

    /**
     * 注册搭配师
     * @param stylist
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/registerstylist")
    public Boolean registerStylist(RequisitionDto stylist) throws BizException{
        Long currentUserId=checkLogin();
        if(null == stylist){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        stylist.setSecUserId(currentUserId);
        return requisitionService.registerRequisition(stylist);
    }

    /**
     * 注册搭配师
     * @param chargeOfStore
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/registerchargeofstore")
    public Boolean registerChargeOfStore(RequisitionDto chargeOfStore) throws BizException{
        Long currentUserId=checkLogin();
        if(null == chargeOfStore){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        chargeOfStore.setSecUserId(currentUserId);
        return requisitionService.registerRequisition(chargeOfStore);
    }
    /**
     * 获取需要审核的用户申请列表
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/getrequisitionauditlist")
    public ResultPagerModel<RequisitionDto> getRequisitionAuditList(QueryRequisitionConditionDto queryCondition) throws BizException{
        //校验登录 TODO 校验用户角色管理员可以审核
        this.checkLogin();
        return requisitionService.getRequisitionAuditList(queryCondition);
    }

    /**
     * 获取用户申请详情
     * @param pkId
     * @return
     * @throws BizException
     */
    @ResponseBody
    @RequestMapping("/getrequisitiondetail")
    public RequisitionDto getRequisitionDetail(Long pkId) throws  BizException{
        if(null==pkId || pkId<=0){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        //校验登录 TODO 校验用户角色管理员可以审核
        this.checkLogin();
        return requisitionService.getRequisitionDetail(pkId);
    }

    @ResponseBody
    @RequestMapping("/requisitionaudit")
    public Boolean requisitionAudit(Long pkId,Boolean isPassed,String message) throws BizException{
        if(null==pkId || pkId<=0 || null==isPassed || StringUtil.isBlank(message)){
            throw new BizException(EnumExceptionCode.EC0001);
        }
        //校验登录 TODO 校验用户角色管理员可以审核
        this.checkLogin();
        return requisitionService.requisitionAudit(pkId,isPassed,message);
    }

}
